import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {path:'/personal',redirect:'/personal/info'},
    // {path:'/',redirect:'/'},
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {title: "首页",},
      children:[
        {
          path: '',
          name: 'index',
          meta: {title: "首页",},
          component: () => import('../views/homeviews/IndexView.vue'),
          children: [
            {
              path: '',
              name: 'commodity',
              component: () => import('../views/homeviews/CommodityView.vue'),
            }
          ]
        },
        {
          path: 'commodity/:id',
          name: 'commodityDetail',
          meta: {title: "商品详情"},
          component: () => import('../views/homeviews/CommodityDetail.vue'),
        },
        {
          path: 'category',
          name: 'category',
          meta: {title: "商品分类",},
          component: () => import('../views/homeviews/CategoryView.vue'),
          children: [
            {
              path: '',
              name: 'search_commodity',
              meta: {title: "商品",},
              component: () => import('../views/homeviews/CommodityView.vue'),
            }
          ]
        },
        {
          path: 'personal',
          name: 'personal',
          meta: {title: "个人中心",},
          component: () => import('../views/Personalviews/PersonalCenter.vue'),
          children:[
              {
                path: 'info',
                name: 'personalinfo',
                meta: {title: "个人信息",},
                component: () => import('../views/Personalviews/Info.vue'),
              },
              {
                path: 'favorites',
                name: 'favorites',
                meta: {title: "我的收藏",},
                component: () => import('../views/Personalviews/FavoritesView.vue'),
              },
              {
                path: 'address',
                name: 'address',
                meta: {title: "收货地址",},
                component: () => import('../views/Personalviews/AddressView.vue'),
              },
              {
                path: 'orders',
                name: 'orders',
                meta: {title: "我的订单",},
                component: () => import('../views/Personalviews/OrdersView.vue'),
              },
              {
                path: 'myarticle',
                name: 'myarticle',
                meta: {title: "我的文章",},
                component: () => import('../views/Personalviews/MyArticleView.vue'),
              },
              
          ]
        },
        {
          path: 'cart',
          name: 'cart',
          meta: {title: "购物车",},
          component: () => import('../views/homeviews/CartView.vue'),
        },
        {
          path: 'checkout',
          name: 'checkout',
          meta: {title: "结算",},
          component: () => import('../views/homeviews/CheckoutView.vue'),
        },
        {
          path: 'order/:id',
          name: 'orderDetail',
          component: () => import('../views/homeviews/OrderDetailView.vue')
        },
        {
          path: 'article',
          name: 'article',
          meta: {title: "校园圈",},
          component: () => import('../views/homeviews/ArticleView.vue'),
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      meta: {title: "登录",},
      component: () => import('../views/Login.vue'),
    },
    {
      path: '/register',
      name: 'register',
      meta: {title: "注册",},
      component: () => import('../views/Register.vue'),
    },
    {path:'/admin',redirect:'/admin/home'},
    {
      path: '/admin',
      name: 'admin',
      meta: {title: "后台",},
      component: () => import('../views/AdminView.vue'),
      children:[
        {
          path: 'home',
          name: 'adminhome',
          meta: {title: "后台",},
          component: () => import('../views/adminviews/AdminHome.vue'),
        },
        {
          path:'usermanagement',
          name:'usermanagement',
          meta: {title: "用户管理",},
          component: () => import('../views/adminviews/UserManagement.vue'),
        },
        {
          path:'loginlog',
          name:'loginlog',
          meta: {title: "登录日志",},
          component: () => import('../views/adminviews/LoginLog.vue'),
        },
        {
          path:'commoditymanagement',
          name:'commoditymanagement',
          meta: {title: "商品管理",},
          component: () => import('../views/adminviews/CommodityManagement.vue'),
        },
        {
          path:'articlemanagement',
          name:'articlemanagement',
          meta: {title: "文章管理",},
          component: () => import('../views/adminviews/ArticleManagement.vue'),
        },
        {
          path:'ordermanagement',
          name:'ordermanagement',
          meta: {title: "订单管理",},
          component: () => import('../views/adminviews/OrderManagement.vue'),
        }
      ]
    },
    {path:'/rider',redirect:'/rider/orders'},
    {
      path: '/rider',
      name: 'rider',
      meta: {title: "骑手中心",},
      component: () => import('../views/riderviews/RiderCenter.vue'),
      children:[
        {
          path: 'orders',
          name: 'riderorders',
          meta: {title: "订单大厅",},
          component: () => import('../views/riderviews/OrdersListView.vue'),
        },
        {
          path: 'history',
          name: 'riderhistory',
          meta: {title: "历史订单",},
          component: () => import('../views/riderviews/HistoryOrders.vue'),
        },
        {
          path: 'info',
          name: 'riderinfo',
          meta: {title: "骑手信息",},
          component: () => import('../views/riderviews/RiderInfo.vue'),
        }
      ]
    },
    {
      path:'/error',
      name:'error',
      meta: {title: "404",},
      component: () => import('../views/ErrorView.vue'),
    },
    {
      path:'/:pathMatch(.*)*',
      redirect:'/error',
    }
  ],
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router